﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 销毁的精灵动画
/// </summary>
public class Sper : MonoBehaviour
{
    //声明一个精灵的集合
    public Sprite[] sprites;
    //精灵的播放速度
    public float speed = 0.016f;
    //用一个时间来记录
    public float time;
    //整数型记录当前播放第几针了
    int curindex;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (sprites != null)
            if (speed > 0)
            {
                time += Time.deltaTime;
                int i = (int)(time / speed);
                //计算当前播放多少帧了,如果当前的帧和上一针的不等的情况下，更新当前的精灵
                if (i != curindex)
                {
                    if (i < sprites.Length)
                    {
                        //更新当前帧
                        curindex = i;
                        //获取当前的精灵呈现器
                        SpriteRenderer sr = GetComponent<SpriteRenderer>();
                        if (sr != null)
                        {
                            sr.sprite = sprites[curindex];
                        }
                    }
                    //如果播放完毕就把预制销毁
                    else
                    {
                        Destroy(gameObject);
                    }
                }
            }
    }
}
